/**
 * The MIT License
 * Copyright (c) 2010 Tad Glines
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package net.towee.server.sio.server.transport;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import net.towee.server.sio.server.SocketIOFrame;

public class TransportBuffer {

	private final int bufferSize;
	private final Semaphore inputSemaphore;
	private final BlockingQueue<SocketIOFrame> queue = new LinkedBlockingQueue<SocketIOFrame>();

	public TransportBuffer(int bufferSize) {
		this.bufferSize = bufferSize;
		this.inputSemaphore = new Semaphore(bufferSize);
	}

	public int getBufferSize() {
		return bufferSize;
	}

	public int getAvailableBytes() {
		return bufferSize - inputSemaphore.availablePermits();
	}

	public int getFreeBytes() {
		return inputSemaphore.availablePermits();
	}

	public boolean isEmpty() {
		return queue.isEmpty();
	}

	public void clear() {
		List<SocketIOFrame> list = new ArrayList<SocketIOFrame>();
		queue.drainTo(list);

		for (SocketIOFrame str : list) {
			inputSemaphore.release(str.getData().length());
		}
	}

	public List<SocketIOFrame> drainMessages() {
		List<SocketIOFrame> list = new ArrayList<SocketIOFrame>();
		queue.drainTo(list);

		for (SocketIOFrame str : list) {
			inputSemaphore.release(str.getData().length());
		}

		return list;
	}

	public SocketIOFrame getMessage(long timeout) {
		try {
			SocketIOFrame msg = queue.poll(timeout, TimeUnit.MILLISECONDS);
			if (msg != null) {
				inputSemaphore.release(msg.getData().length());
			}
			return msg;
		} catch (InterruptedException e) {
			return null;
		}
	}

	public boolean putMessage(SocketIOFrame message, long timeout) {
		try {
			if (!inputSemaphore.tryAcquire(message.getData().length(), timeout, TimeUnit.MILLISECONDS)) {
				return false;
			}
			queue.offer(message);
			return true;
		} catch (InterruptedException e) {
			return false;
		}
	}
}
